package com.example.cardgame

import android.content.Intent
import android.os.Bundle
import android.widget.RadioButton
import java.util.ArrayList;
import java.util.List;
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.cardgame.databinding.ActivityEditBinding

class EditActivity : AppCompatActivity() {
    private val binding by lazy { ActivityEditBinding.inflate(layoutInflater) }
    var gender = ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(binding.main) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        val launcherPhoto = registerForActivityResult(ActivityResultContracts.TakePicturePreview()) {
            binding.avatar.setImageBitmap(it)
        }
        binding.photoButton.setOnClickListener{
            launcherPhoto.launch(null)
        }

        binding.radioGender.setOnCheckedChangeListener { _, checkedId ->
            val selectedRadioButton: RadioButton = findViewById(checkedId)
            gender = selectedRadioButton.text.toString()

        }

        binding.buttonSave.setOnClickListener {
            val intent = Intent()
            val hobbies = getSelectedHobbies()
            val person = Person(
                name = binding.editNameText.text.toString(),
                gender = gender,
                sid = binding.editSidText.text.toString(),
                phone = binding.editPhoneText.text.toString(),
                email = binding.editEmailText.text.toString(),
                hobby = hobbies, score = 0)
            intent.putExtra(KEY_PROFILE, person)
            setResult(RESULT_OK, intent)
            finish()
        }

        binding.buttonCancel.setOnClickListener {
            finish()
        }
    }

    fun getSelectedHobbies(): String {
        val hobbies = StringBuilder()

        if (binding.checkBoxTravel.isChecked) {
            hobbies.append(binding.checkBoxTravel.text.toString()).append(" | ")
        }
        if (binding.checkBoxRead.isChecked) {
            hobbies.append(binding.checkBoxRead.text.toString()).append(" | ")
        }
        if (binding.checkBoxGame.isChecked) {
            hobbies.append(binding.checkBoxGame.text.toString()).append(" | ")
        }
        if (binding.checkBoxShopping.isChecked) {
            hobbies.append(binding.checkBoxShopping.text.toString()).append(" | ")
        }

        if (hobbies.isNotEmpty()) {
            hobbies.setLength(hobbies.length - 3)
        }

        return hobbies.toString()
    }
}